Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

focusable-selectors

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

focusable-selectors

A list of CSS selectors for focusable elements

  • 0.8.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Focusable Selectors

focusable-selectors is a micro-lib exporting an array of CSS selectors for focusable HTML elements. Figuring out which element can be focused is not always trivial and is sometimes necessary to build fully accessible widgets such as a11y-dialog.

It supports:

  • All natively focusable elements,
  • … and any element with the contenteditable attribute,
  • … provided they are not disabled,
  • … and do not have a negative tabindex attribute,
  • … and do not have the inert attribute.

For a more advanced solution using JavaScript and covering more edge cases, refer to focus-trap/tabbable.

Usage

npm install --save focusable-selectors
import focusableSelectors from 'focusable-selectors'

To have a single CSS selector out of it, join the array with commas:

const selector = selectors.join(',')
const focusableElements = document.querySelectorAll(selector)

Considering styles

The “focusability” of an element is not solely based on its type, but also on its styles. A relatively straightforward solution is to use JavaScript to figure out whether focusable elements are effectively visible by checking their dimensions.

function isVisible(element) {
  return Boolean(
    element.offsetWidth ||
      element.offsetHeight ||
      element.getClientRects().length
  )
}

const selector = selectors.join(',')
const focusableElements = Array.from(
  document.querySelectorAll(selector)
).filter(isVisible)

Keywords

FAQs

Package last updated on 04 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc